package demo1;

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
     ListNode fast = head ;
     ListNode slow = head ;
     ListNode slowPrev = null ;
     while(n>1){
         fast = fast.next ;
         n -- ;
     }
     while(fast.next!=null){
        slowPrev = slow ;
         fast = fast.next ;
         slow = slow.next ;
     }
     if(slowPrev != null){
       slowPrev.next = slow.next ;
     }else{
     head = slow.next ;
     }
      return head ;
    }
}